package com.sias.realestate.web.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.sias.realestate.web.model.Message;
import com.sias.realestate.web.model.Owner;
import com.sias.realestate.web.model.User;
import com.sias.realestate.web.service.MessageService;
import com.sias.realestate.web.service.OwnerService;
import com.sias.realestate.web.service.UserService;
import com.sias.realestate.web.util.FinalVarUtil;
import com.sias.realstate.web.pagemodel.PageModel;

@Controller
@RequestMapping(value = "/message")
public class MessageController {

	@Resource
	private MessageService messageService;
	@Resource
	private OwnerService ownerService;
	@Resource
	private UserService userService;

	@RequestMapping(value = "/getAllMessage", method = RequestMethod.GET)
	public ModelAndView getAllMessage(HttpServletRequest request)
			throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		ModelAndView mv = new ModelAndView();
		PageModel pageModel = new PageModel();
		List<Message> list = messageService.selectList();
		pageModel.setTh_list(FinalVarUtil.messageTh);
		mv.addObject("pageModel", pageModel);
		mv.addObject("list", list);
		mv.setViewName("message/messageList");
		return mv;
	}

	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：我的投诉维护信息 创建日期：2016年12月13日下午4:41:52
	 */
	@RequestMapping(value = "/getMyMessage", method = RequestMethod.GET)
	public ModelAndView getMyMessage(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		PageModel pageModel = new PageModel();
		User user = (User) request.getSession().getAttribute("user");
		Owner owner = ownerService.findByIdcard(user.getuIdcard());
		List<Message> list = null;
		if (owner != null) {
			pageModel.setTh_list(FinalVarUtil.mymessageTh);
			list = messageService.findMessageByOid(owner.getoId());// 根据oid获取我的投诉/维护信息
			mv.setViewName("message/mymessageList");
		} else {
			pageModel.setTh_list(FinalVarUtil.messageTh);// 获取我的投诉/维护信息表头
			mv.setViewName("message/messageList");
		}
		mv.addObject("pageModel", pageModel);
		mv.addObject("list", list);
		return mv;
	}

	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：跳转到addMessage界面 创建日期：2016年12月13日下午4:40:59
	 */
	@RequestMapping(value = "/addMessageJsp", method = RequestMethod.GET)
	public ModelAndView addMessageJsp(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("message/addmessage");
		return mv;
	}

	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：添加投诉维护信息 创建日期：2016年12月13日下午5:10:47
	 */
	@RequestMapping(value = "/addMessage", method = RequestMethod.POST)
	public ModelAndView addMessage(HttpServletRequest request, String type, String content, String remark) {
		Message message = new Message();
		User user = (User) request.getSession().getAttribute("user");
		Owner owner = ownerService.findByIdcard(user.getuIdcard());
		message.setoId(owner.getoId());
		message.setmType((Integer.parseInt(type) - 1));
		message.setmContent(content);
		message.setmRemark(remark);
		message.setmTime(new Date());
		message.setmStatus(0);
		messageService.insert(message);
		ModelAndView mv = new ModelAndView();
		request.setAttribute("successInfo", "添加信息成功！");
		request.setAttribute("page_code", "allmessage");
		mv.setViewName("/success-info");

		return mv;
	}

	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：撤销投诉维护信息 创建日期：2016年12月13日下午5:10:47
	 */
	@RequestMapping(value = "/cancelMessage", method = RequestMethod.GET)
	public ModelAndView cancelMessage(HttpServletRequest request, Integer mid) {
		ModelAndView mv = new ModelAndView();
		PageModel pageModel = new PageModel();
		messageService.delete(mid);
		User user = (User) request.getSession().getAttribute("user");
		Owner owner = ownerService.findByIdcard(user.getuIdcard());
		List<Message> list = messageService.findMessageByOid(owner.getoId());
		pageModel.setTh_list(FinalVarUtil.mymessageTh);// 获取我的投诉/维护信息表头
		mv.addObject("pageModel", pageModel);
		mv.addObject("list", list);
		mv.setViewName("message/mymessageList");
		return mv;
	}

	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：将投诉维护信息修改为已解决 创建日期：2016年12月13日下午5:12:16
	 */
	@RequestMapping(value = "/changeMessageStatus", method = RequestMethod.GET)
	public ModelAndView changeMessageStatus(HttpServletRequest request, Integer mid) {
		ModelAndView mv = new ModelAndView();
		PageModel pageModel = new PageModel();
		Message message = messageService.selectById(mid);
		message.setmStatus(1);
		messageService.update(message);
		User user = (User) request.getSession().getAttribute("user");

		if (user.getuStatus().equals("0")) {
			List<Message> list = messageService.selectList();
			pageModel.setTh_list(FinalVarUtil.messageTh);
			mv.addObject("pageModel", pageModel);
			mv.addObject("list", list);
			mv.setViewName("message/messageList");
		}
		if (user.getuStatus().equals("1")) {
			Owner owner = ownerService.findByIdcard(user.getuIdcard());
			List<Message> list = messageService.findMessageByOid(owner.getoId());
			pageModel.setTh_list(FinalVarUtil.mymessageTh);// 获取我的投诉/维护信息表头
			mv.addObject("pageModel", pageModel);
			mv.addObject("list", list);
			mv.setViewName("message/mymessageList");
		}
		return mv;
	}

}
